{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5LJ86dPrDKNi"
      },
      "outputs": [],
      "source": [
        "# 配置环境（务必启动这个单元格！等运行完毕再点下面的单元格）\n",
        "\n",
        "backend='TensorRT' # 跑谱使用的后端，可填TensorRT（trt）、CUDA\n",
        "backend_is_available=['cuda','trt','tensorrt']\n",
        "if backend.lower() not in backend_is_available:\n",
        "  raise ValueError('backend的值无效，请检查拼写。')\n",
        "\n",
        "# 安装libssl，大概需要2s时间\n",
        "%cd ~\n",
        "!wget https://github.com/FengmingGo/katago_for_colaboratory/releases/download/20241228/libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb\n",
        "!dpkg -i libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb && rm libssl1.1_1.1.1f-1ubuntu2.17_amd64.deb\n",
        "\n",
        "# 安装libzip，大概需要20s时间\n",
        "!wget https://libzip.org/download/libzip-1.11.2.tar.gz\n",
        "!tar -zxvf libzip-1.11.2.tar.gz && rm libzip-1.11.2.tar.gz\n",
        "%cd libzip-1.11.2\n",
        "!cmake .\n",
        "!make && make install\n",
        "!ldconfig /usr/local/lib && ldconfig /usr/local/lib64\n",
        "%cd ..\n",
        "!rm -rf libzip-1.11.2\n",
        "\n",
        "# 根据backend的值配置相关环境\n",
        "if backend.lower() == 'cuda':\n",
        "  !apt-get install libcudnn8=8.9.7.29-1+cuda12.2 # 由于colab默认环境不再是libcudnn8，不得不重新安装libcudnn8，大概需要半分钟时间。\n",
        "  !wget https://github.com/lightvector/KataGo/releases/download/v1.15.3/katago-v1.15.3-cuda12.1-cudnn8.9.7-linux-x64.zip\n",
        "  !unzip katago-v1.15.3-cuda12.1-cudnn8.9.7-linux-x64.zip && rm katago-v1.15.3-cuda12.1-cudnn8.9.7-linux-x64.zip\n",
        "elif backend.lower() == 'tensorrt' or backend.lower() == 'trt':\n",
        "  !apt-get install libnvinfer10=10.2.0.19-1+cuda12.5 # 配置libnvinfer10，这大约需要两分钟时间。\n",
        "  !wget https://github.com/lightvector/KataGo/releases/download/v1.15.3/katago-v1.15.3-trt10.2.0-cuda12.5-linux-x64.zip\n",
        "  !unzip katago-v1.15.3-trt10.2.0-cuda12.5-linux-x64.zip && rm katago-v1.15.3-trt10.2.0-cuda12.5-linux-x64.zip\n",
        "  !mkdir -p /root/.katago/trtcache/\n",
        "  %cd /root/.katago/trtcache\n",
        "  !wget https://github.com/FengmingGo/katago_for_colaboratory/releases/download/20250108/trt-100200_gpu-131365bf_tune-cf9c2f523082_max19x19_batch8_fp16\n",
        "  !wget https://github.com/FengmingGo/katago_for_colaboratory/releases/download/20250108/trt-100200_gpu-131365bf_tune-fcabb5a49c9c_max19x19_batch8_fp16\n",
        "\n",
        "# 替换跑谱用的配置文件\n",
        "%cd ~\n",
        "!rm contribute_example.cfg && wget https://github.com/FengmingGo/katago_for_colaboratory/releases/download/20241228/contribute_example.cfg\n",
        "print('配置环境完毕，可运行下个单元格。')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "kerjDL1w45QM"
      },
      "outputs": [],
      "source": [
        "# 跑谱\n",
        "username = \"凤鸣围棋\" # 用户名，注意在跑谱之前别忘了邮箱认证\n",
        "password = \"gtx1660s\" # 密码，katago账户的密码\n",
        "!./katago contribute -config contribute_example.cfg -override-config username={username},password={password} # 开始跑谱（同时跑10张谱）"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}